package com.leoman.plz_p.views.study;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.leoman.plz_p.R;
import com.leoman.plz_p.adapter.AnswerAdapter;
import com.leoman.plz_p.constants.Constants;
import com.leoman.plz_p.interfaces.AnswerQuestionView;
import com.leoman.plz_p.module.bean.QuestionBean;
import com.leoman.plz_p.presenter.AnswerQuestionPresenter;

import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import cn.com.corelib.base.BaseFragment;
import cn.com.corelib.views.NoScrollingListView;

/**
 * 答题
 */
public class AnswerQuestionFragment extends BaseFragment<AnswerQuestionView, AnswerQuestionPresenter> implements AnswerQuestionView {

    @Bind(R.id.tv_question_type)
    TextView tvQuestionType;
    @Bind(R.id.iv_question_image)
    ImageView ivQuestionImage;
    @Bind(R.id.ll_answers)
    NoScrollingListView llAnswers;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_explain)
    TextView tvExplain;
    @Bind(R.id.ll_explain)
    LinearLayout llExplain;
    private AnswerAdapter adapter;

    private boolean showExplain;

    private String questionId;

    private QuestionBean questionBean;

    private String answer;

    private boolean hasAnswer;

    private boolean isNotNormalExam;

    @Override
    protected int getLayoutId() {
        return R.layout.fr_answer_question;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        Bundle bundle=getArguments();
        showExplain=  bundle.getBoolean(Constants.KEY_SHOW_EXPLAIN);
        questionId= bundle.getString(Constants.KEY_QUESTION_ID);
        answer= bundle.getString(Constants.KEY_ANSWER);
        isNotNormalExam =bundle.getBoolean(Constants.KEY_ANSWER_ERROR_SHOW_RIGHT,true);
        hasAnswer=bundle.getBoolean(Constants.KEY_HAS_ANSWER,false);
        getPresenter().getQuestionDetail(questionId);
        adapter = new AnswerAdapter(getViewContext());
        adapter.setIsNotNormalExam(isNotNormalExam);
        llAnswers.setAdapter(adapter);
        llAnswers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!isNotNormalExam){
                    if (adapter.getItem(i).getAnswerRight()!=null){
                        adapter.getItem(i).setAnswerRight(null);
                        StringBuffer buffer=new StringBuffer("");
                        for (int j = 0; j <adapter.getData().size() ; j++) {
                            if (adapter.getData().get(j).getAnswerRight()!=null){
                                buffer.append((j+1)+",");
                            }
                        }
                        if (TextUtils.isEmpty(buffer)){
                            questionAnswerRight.answerNotNeedCalculate(i,null,null);
                        }else {
                            String s=buffer.subSequence(0,buffer.length()).toString();
                            boolean allAnswerRight=true;
                            for (int j = 0; j <adapter.getData().size() ; j++) {
                                if (adapter.getData().get(j).isRight()&&adapter.getData().get(j).getAnswerRight()!=null&&adapter.getData().get(j).getAnswerRight()){

                                }else {
                                    allAnswerRight=false;
                                }
                            }
                            questionAnswerRight.answerNotNeedCalculate(i,s,allAnswerRight);
                        }

                        adapter.notifyDataSetChanged();
                    }else {
                        if (adapter.getItem(i).isRight()){
                            adapter.getItem(i).setAnswerRight(true);
                        }else {
                            adapter.getItem(i).setAnswerRight(false);
                        }
                        StringBuffer buffer=new StringBuffer("");
                        for (int j = 0; j <adapter.getData().size() ; j++) {
                            if (adapter.getData().get(j).getAnswerRight()!=null){
                                buffer.append((j+1)+",");
                            }
                        }
                        if (!TextUtils.isEmpty(buffer)){
                            String s=buffer.subSequence(0,buffer.length()).toString();
                            boolean allAnswerRight=true;
                            for (int j = 0; j <adapter.getData().size() ; j++) {
                                if (adapter.getData().get(j).isRight()&&adapter.getData().get(j).getAnswerRight()!=null&&adapter.getData().get(j).getAnswerRight()){

                                }else {
                                    allAnswerRight=false;
                                }
                            }
                            questionAnswerRight.answerNotNeedCalculate(i,s,allAnswerRight);
                        }
                        adapter.notifyDataSetChanged();
                    }
                }else {
                    if (hasAnswer) return;
                    //答题之后不再作答
                    if (adapter.isHasAnswered()) return;

                    //未答题
                    if (adapter.getItem(i).isRight()) {
                        adapter.getItem(i).setAnswerRight(true);
                        boolean allAnswer = true;
                        for (int j = 0; j < adapter.getData().size(); j++) {
                            if (adapter.getData().get(j).isRight() && adapter.getItem(j).getAnswerRight() == null) {
                                allAnswer = false;
                            }
                        }
                        if (allAnswer) {
                            adapter.setHasAnswered(true);
                            questionAnswerRight.answerIsRight(i);
                        }
                    } else {
                        questionAnswerRight.answerIsError(i);
                        adapter.getItem(i).setAnswerRight(false);
                        adapter.setHasAnswered(true);
                    }
                    adapter.notifyDataSetChanged();
                    if (showExplain) {
                        llExplain.setVisibility(View.VISIBLE);
                    } else {
                        llExplain.setVisibility(View.GONE);
                    }
                }
            }
        });


    }

    @Override
    protected AnswerQuestionPresenter createPresenter() {
        return new AnswerQuestionPresenter();
    }


    private onQuestionAnswerRight questionAnswerRight;

    public void setOnAnswerListener(onQuestionAnswerRight onAnswerListener) {
        this.questionAnswerRight = onAnswerListener;
    }

    @Override
    public void obtainQuestionDetail(QuestionBean data) {
        this.questionBean=data;
        tvTitle.setText(data.getTopic().getName());
        List<String> right= Arrays.asList(data.getTopic().getIsTrue().split(","));
        for (int i = 0; i <right.size(); i++) {
            data.getList().get(Integer.parseInt(right.get(i))-1).setIsTrue(true);
        }
        adapter.replaceAll(data.getList());
        switch (data.getTopic().getChoice()){
            case 1:
                tvQuestionType.setText("单选题");
                break;
            case 2:
                tvQuestionType.setText("多选题");
                break;
            case 3:
                tvQuestionType.setText("判断题");
                break;
        }
        if (!TextUtils.isEmpty(data.getTopic().getImage())){
            ivQuestionImage.setVisibility(View.VISIBLE);
            Glide.with(getViewContext()).load(data.getTopic().getImage()).into(ivQuestionImage);
        }
        tvExplain.setText("官方解读:"+data.getTopic().getReach());

        if (hasAnswer){
            if (!TextUtils.isEmpty(answer)) {
                List<String> answers =Arrays.asList(answer.split(","));
                for (int i = 0; i <answers.size() ; i++) {
                    if (adapter.getItem(Integer.parseInt(answers.get(i))-1).getIsTrue()!=null&&adapter.getItem(Integer.parseInt(answers.get(i))-1).getIsTrue()){
                        adapter.getItem(Integer.parseInt(answers.get(i))-1).setAnswerRight(true);
                    }
                    if (adapter.getItem(Integer.parseInt(answers.get(i))-1).getIsTrue()==null||!adapter.getItem(Integer.parseInt(answers.get(i))-1).getIsTrue()){
                        adapter.getItem(Integer.parseInt(answers.get(i))-1).setAnswerRight(false);
                    }
                }
                adapter.notifyDataSetChanged();
            }
        }
    }


    public interface onQuestionAnswerRight {
        void answerIsRight(int position);
        void answerIsError(int position);

        void answerNotNeedCalculate(int position,String answers,Boolean isRight);
    }
}